﻿/// <reference path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\MicrosoftAjax.js" />
/// <reference path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\SP.debug.js" />
/// <reference path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\SP.Ribbon.debug.js" />
/// <reference path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\SP.Core.debug.js" />
/// <reference path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\SP.UI.debug.js" />
/// <reference path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\SP.UI.Dialog.debug.js" />

function HasUnapprovedSelectedItems() {
    var selectedItems = SP.ListOperation.Selection.getSelectedItems();
    return selectedItems.length > 0;
}

function HasUnapprovedItems() {
    return false;
}

function ApproveItems() {
    var clientContext = SP.ClientContext.get_current();
    var site = clientContext.get_web();
    var listId = SP.ListOperation.Selection.getSelectedList();
    var list = site.get_lists().getById(listId);
    var selectedItems = SP.ListOperation.Selection.getSelectedItems();
    for (i = 0; i < selectedItems.length;i++ )
    {
        var itemId = selectedItems[i].id;
        var item = list.getItemById(itemId);
        item.set_item('_ModerationStatus', 0);
        item.set_item('_ModerationComments', 'Approved');
        item.update();
    }
    clientContext.executeQueryAsync(onApproveSucceeded, onQueryFailed);
}

function DeleteUnapprovedItems() {
debugger
    var clientContext = SP.ClientContext.get_current();
    var site = clientContext.get_web();
    var listId = SP.ListOperation.Selection.getSelectedList();
    var list = site.get_lists().getById(listId);
    var query = new SP.CamlQuery();
    query.set_viewXml('<View><Where><Eq><FieldRef Name="_ModerationStatus" /><Value Type="ModStat">2</Value></Eq></Where></View>');
    _itemsToDelete = list.getItems(query);
    clientContext.load(_itemsToDelete);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQueryForUnapprovedItemsSucceeded),
        Function.createDelegate(this, this.onQueryFailed));
}
var _itemsToDelete;

function onQueryForUnapprovedItemsSucceeded() {
    var enumerator = _itemsToDelete.getEnumerator();
    var mainDivElement = document.createElement('div');
    while (enumerator.moveNext()) {
        var item = enumerator.get_current();
        var title = item.get_item('Title');
        var id = item.get_item('ID');
        var itemDivElement = document.createElement('div');
        itemDivElement.innerHTML = title;
        mainDivElement.appendChild(itemDivElement);
    }
    var buttonDivElement = document.createElement('div')
    var buttonElement = document.createElement('input');
    buttonElement.setAttribute('type', 'button');
    buttonElement.setAttribute('value', 'Delete them!');
    buttonElement.setAttribute('onclick', 
        'SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK, null);');
    buttonDivElement.appendChild(buttonElement);
    mainDivElement.appendChild(buttonDivElement);
    var options = SP.UI.$create_DialogOptions();
    options.width = 500;
    options.height = 250;
    options.title = "Delete these items?";
    options.html = mainDivElement;
    options.showClose = true;
    options.dialogReturnValueCallback = Function.createDelegate(
                    this, this.onDialogClose);
    SP.UI.ModalDialog.showModalDialog(options);
}

function onDialogClose(dialogResult, returnValue) {
    if (dialogResult === SP.UI.DialogResult.OK) {
        var enumerator = _itemsToDelete.getEnumerator();
        var tempArray = new Array()       
        while (enumerator.moveNext()) {
            tempArray.push(enumerator.get_current());
        }
        for (var i = 0; i < tempArray.length; i++) {
            tempArray[i].deleteObject();
        }
        var clientContext = SP.ClientContext.get_current();
        clientContext.executeQueryAsync(
            Function.createDelegate(this, this.onDeleteSucceeded),
            Function.createDelegate(this, this.onQueryFailed));
    } else {
        SP.UI.Notify.addNotification("This was stopped", false);
    }
}

function onApproveSucceeded() {
    RefreshPage(SP.UI.DialogResult.OK);
}

function onDeleteSucceeded(){
    RefreshPage(SP.UI.DialogResult.OK);
}

function onQueryFailed(sender, args) {
    var statusId = SP.UI.Status.addStatus("Action failed",  "Stuff went wrong");
    SP.UI.Status.setStatusPriColor(statusId, 'red');
}